<template>
  <div>
    <div class="searchLists">
      <div class="header">
        <div class="left iconfont icon-fanhuipt" @click="$router.go(-1)"></div>

        <div class="ipt">
          <input
            v-model="search"
            type="text"
            placeholder="搜索音乐、视频、播客、歌词"
            @keyup.enter="$router.push('/search/searchList?keywords=' + search)"
          />
          <div v-show="search" class="iconfont icon-x" @click="searchDel"></div>
        </div>
      </div>

      <!-- 搜索结果 -->
      <div class="result">
        <div>
          <ul>
            <div>
              <h3>单曲</h3>
            </div>

            <li v-for="(itemRes, index) in searchRes.songs" :key="index">
              <div class="left">
                <h4>{{ itemRes.name }}</h4>
                <p>
                  <span>{{ itemRes.ar[0].name }}</span> {{ itemRes.al.name }}
                </p>
                <p>{{ itemRes.alia }}</p>
              </div>

              <div class="right">
                <div class="iconfont icon-shipin"></div>
                <div class="iconfont icon-gengduo"></div>
              </div>
            </li>
          </ul>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {
  getSearchlist,
  getSearchKeyword,
  getCloudSearch,
} from "../../api/search";
import { Dialog } from "vant";

export default {
  data() {
    return {
      search: null,
      hotlist: null,
      list: null,
      hotUpBtn: "ture",
      suggestion: {
        artists: null,
        songs: null,
        albums: null,
        playlists: null,
      },
      record: [],
      searchRes: {
          songs: null,
      },
    };
  },
  methods: {
    SearchlistFun() {
      getSearchlist().then((data) => {
        console.log(data);
        this.list = data.data;
        this.hotlist = data.data.slice(0, 10); //不会修改原数组
        // console.log(this.hotlist.splice(10)); 会修改原数组
        console.log(this.hotlist);
        console.log(this.list);
      });
    },
    hotUp() {
      this.hotlist = this.list;
      this.hotUpBtn = false;
    },
    searchDel() {
      this.search = null;
    },
    historyDelAll() {
      Dialog.confirm({
        // title: "",
        message: "确定清空全部历史记录？",
      })
        .then(() => {
          this.record = [];
        })
        .catch(() => {
          // on cancel
        });
    },
    historyDel(index) {
      this.record.splice(index, 1);
    },

    add() {
      this.record.push(this.search);
      console.log(this.record);
    },

    getNavsBtnFun() {
      this.$emit("getnavs-btn", true);
    },
  },

  created() {
    this.SearchlistFun();
    this.$emit("getnavs-btn", false);
  // },
  // () {
    getCloudSearch({ keywords: this.search }).then((data) => {
      console.log(data);
      this.searchRes = data.result;
    });
  },
  // computed: {},
  watch: {
    search() {
      getSearchKeyword({ keywords: this.search }).then((data) => {
        console.log(data);
        this.suggestion = data.result;
      });
    },
  },
};
</script>

<style lang="less">
.searchLists {
  .header {
    padding-top: 4px;
    padding-bottom: 8px;
    background-color: #d44439;
    height: 39px;
    line-height: 39px;
    text-align: center;
    display: flex;
    .left {
      flex: 0 0 40px;
      font-size: 18px;
    }
    .ipt {
      flex: 1;
      margin-right: 15px;
      overflow: hidden;
      position: relative;
      input {
        width: 100%;
        border: 0px;
        border-bottom: 1px solid #000;
        background-color: transparent;
      }
      input::-webkit-input-placeholder {
        color: #fff;
      }
      .iconfont {
        position: absolute;
        top: 0;
        right: 0;
        font-size: 24px;
      }
    }
  }
  .content {
    width: 100%;
    min-height: 50px;
    padding: 10px;
    box-sizing: border-box;
    h3 {
      font-size: 20px;
      font-weight: bold;
      padding: 10px 0;
    }
    ul {
      width: 100%;
      border-radius: 5px;
      // background-color: aquamarine;
      padding: 10px;
      box-sizing: border-box;
      box-shadow: 0 0px 3px 2px rgba(0, 0, 0, 0.1);
      display: flex;
      flex-wrap: wrap;
      li {
        width: 50%;
        display: flex;
        margin-bottom: 10px;
        p {
          font-size: 14px;
          padding: 0 5px;
          display: -webkit-box;
          // 3、弹性盒子排列方式：垂直排列：
          -webkit-box-orient: vertical;
          // 4、规定显示几行文本：
          -webkit-line-clamp: 1;
          // 5、溢出隐藏：
          overflow: hidden;
        }
        .nbsp {
          padding-right: 9px;
        }
        img {
          width: 100%;
          height: 10px;
        }
      }
      .btn {
        width: 100%;
        text-align: center;
        font-size: 14px;
        color: #aaa;
        padding-top: 5px;
      }
      .con {
        span {
          color: #d44439;
        }
        p {
          font-weight: bold;
        }
      }
    }
  }
  .history {
    .title {
      display: flex;
      justify-content: space-between;
      margin: 0 10px;
      margin-bottom: 10px;
      h3 {
        font-size: 20px;
        text-align: center;
      }
      .iconfont {
        font-size: 30px;
      }
    }
    ul {
      li {
        display: flex;
        justify-content: space-between;
        margin: 0 10px;
        font-size: 14px;
        margin-bottom: 5px;
        .iconfont {
          font-size: 20px;
          padding-right: 5px;
          text-align: center;
        }
      }
    }
  }
  .item {
    width: 100%;
    height: 100%;
    background-color: #fff;
    position: absolute;
    top: 40px;
    left: 0;
    ul {
      li {
        display: flex;
        .iconfont {
          flex: 0 0 40px;
          text-align: center;
          line-height: 50px;
          font-size: 23px;
        }
        p {
          flex: 1;
          padding: 15px 0;
          border-bottom: 1px solid #ccc;
        }
      }
    }
  }
}
</style>